package com.yupi.oj.judge.strategy;

import cn.hutool.json.JSONUtil;
import com.yupi.oj.model.dto.question.JudgeCase;
import com.yupi.oj.model.dto.question.JudgeConfig;
import com.yupi.oj.judge.codesanbox.model.JudgeInfo;
import com.yupi.oj.model.entity.Question;
import com.yupi.oj.model.enums.JudgeInfoMessageEnum;

import java.util.List;

/**
 * @Author FengHuan Wang
 * @Date 2025/3/9 13:32
 * @Version 1.0
 */
public class JavaStrategy implements JudgeStrategy{
    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext) {

        JudgeInfo judgeInfo = judgeContext.getJudgeInfo();
        List<String> inputList = judgeContext.getInputList();
        List<String> outputList = judgeContext.getOutputList();
        Question question = judgeContext.getQuestion();
        Long memory = judgeInfo.getMemoryLimit();
        Long time = judgeInfo.getTimeLimit();
        List<JudgeCase> judgeCaseList = judgeContext.getCaseList();
        JudgeInfoMessageEnum messageEnum = JudgeInfoMessageEnum.ACCEPTED;
        JudgeInfo judgeInfoResponse = new JudgeInfo();
        judgeInfoResponse.setMemoryLimit(memory);
        judgeInfoResponse.setTimeLimit(time);
        judgeInfoResponse.setMessage(messageEnum.getValue());
        //判断代码沙箱的执行结果是否和预期输出结果相等
        if (outputList.size() != inputList.size()){
            messageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
            return judgeInfoResponse;
        }
        for (int i = 0; i < judgeCaseList.size(); i++) {
            JudgeCase judgeCase1 = judgeCaseList.get(i);
            if (!judgeCase1.getOutput().equals(outputList.get(i))){
                messageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
                return judgeInfoResponse;
            }
        }
        //判断题目限制
        // TODO 判断题目限制逻辑
        String judgeConfig = question.getJudgeConfig();
        JudgeConfig judgeConfig1 = JSONUtil.toBean(judgeConfig, JudgeConfig.class);
        long memoryLimit = judgeConfig1.getMemoryLimit();
        long timeLimit = judgeConfig1.getTimeLimit();
        if (memory > memoryLimit){
            messageEnum = JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED;
            return null;
        }
        //java执行耗时
        long JAVA_TIME = 10000L;
        if (time - JAVA_TIME > timeLimit){
            messageEnum = JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED;
            return judgeInfoResponse;
        }

        return judgeInfoResponse;
    }
}
